package com.gf.fund.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.common.bean.Page;
import com.gf.fund.dao.FundDao;
import com.gf.fund.mapper.FundMapper;
import com.gf.fund.model.Fund;

/**
 * 
 * @author zhuguifeng
 * 
 */
@Service("fundDao")
public class FundDaoImpl extends BaseDaoImpl implements FundDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * insert into fund (userId,last,changes,total,createTime) values
	 * (?,?,?,?,current_timestamp)
	 */
	@Value("${fundDao.addFundSql}")
	private String addFundSql;
	/**
	 * select total from fund where id=(select max(id) from fund where userId=?)
	 */
	@Value("${fundDao.queryTotal}")
	private String queryTotal;

	/**
	 * select * from fund where userId=? and changes<0 order by createTime desc
	 */
	@Value("${fundDao.queryConsumeFundByUserIdSql}")
	private String queryConsumeFundByUserIdSql;

	/**
	 * select * from fund where userId=? and changes<0 and
	 * unix_timestamp(createTime)>=? and unix_timestamp(createTime)<=? order by
	 * createTime desc
	 */
	@Value("${fundDao.queryConsumeFundByUserIdWithTimeSql}")
	private String queryConsumeFundByUserIdWithTimeSql;

	/**
	 * select * from fund where userId=? and changes>0 order by createTime desc
	 */
	@Value("${fundDao.queryChargeFundByUserIdSql}")
	private String queryChargeFundByUserIdSql;

	/**
	 * select * from fund where userId=? and changes>0 and
	 * unix_timestamp(createTime)>=? and unix_timestamp(createTime)<=? order by
	 * createTime desc
	 */
	@Value("${fundDao.queryChargeFundByUserIdWithTimeSql}")
	private String queryChargeFundByUserIdWithTimeSql;

	/**
	 * select * from fund where userId=? order by createTime desc
	 */
	@Value("${fundDao.queryFundByUserIdSql}")
	private String queryFundByUserIdSql;

	@Override
	public long insertFund(long userId, double last, double changes,
			double total, String event) {
		return this.insertWithIdReturn(addFundSql, userId, last, changes,
				total, event);
	}

	@Override
	public double queryTotal(long userId) {
		return jdbcTemplate.queryForObject(queryTotal, new Object[] { userId },
				Double.class);
	}

	// mysql unix_timestamp 是秒数
	// js new Date().getTime() 是ms
	@Override
	public List<Fund> queryConsumeFundByUserId(long userId, Page page,
			long startTime, long endTime) {
		// 对时间没有要求
		if (startTime == 0 && endTime == 0) {
			return getPaginationResult(queryConsumeFundByUserIdSql, page,
					FundMapper.getMapper(), userId);
		}
		return getPaginationResult(queryConsumeFundByUserIdWithTimeSql, page,
				FundMapper.getMapper(), userId, startTime / 1000,
				endTime / 1000);
	}

	@Override
	public List<Fund> queryChargeFundByUserId(long userId, Page page,
			long startTime, long endTime) {
		// 对时间没有要求
		if (startTime == 0 && endTime == 0) {
			return getPaginationResult(queryChargeFundByUserIdSql, page,
					FundMapper.getMapper(), userId);
		}
		return getPaginationResult(queryChargeFundByUserIdWithTimeSql, page,
				FundMapper.getMapper(), userId, startTime / 1000,
				endTime / 1000);
	}

	@Override
	public List<Fund> queryFundByUserId(long userId, Page page) {

		return getPaginationResult(queryFundByUserIdSql, page,
				FundMapper.getMapper(), userId);
	}

}
